package ro.isdc.navigator.retriever.jdbc.strategy;

/**
 * MySQL strategy for obtaining the paginated SQL.
 * 
 * @author Andrei Chirila / ISDC! Romania
 * @version 1.0
 * @date Jul 8, 2009
 */
public class MySQLComputePaginatedSQLStrategy extends AbstractComputePaginatedSQLStrategy {

  private static final String LIMIT_CLAUSE_INDEXED = " LIMIT ?, ?";

  private static final String LIMIT_CLAUSE_PARAM = " LIMIT :start, :end";

  /**
   * {@inheritDoc}
   */
  public PaginatedSQL computePaginatedSQL(String SQL, SQLType sqlType,
      int startIndex, int blockSize) {

    PaginatedSQL generatedSQL = super.computePaginatedSQL(SQL, sqlType,
        startIndex, blockSize);
    generatedSQL.setLeftLimit(startIndex);
    generatedSQL.setRightLimit(blockSize);
    return generatedSQL;
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  protected String buildPaginatedSQL(final String SQL, final SQLType sqlType) {
    StringBuffer sqlBuffer = new StringBuffer(SQL);
    if (SQLType.INDEXED == sqlType) {
      sqlBuffer.append(LIMIT_CLAUSE_INDEXED);
    } else {
      sqlBuffer.append(LIMIT_CLAUSE_PARAM);
    }
    
    return sqlBuffer.toString();
  }
}
